package com.shujia.mr.clazzSort2;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class ClazzSortMapper extends Mapper<LongWritable, Text,Text,StudentMap> {
    /*
        数据处理逻辑：
            1.读取ReduceJoin中的数据
            2.对数据进行切分，将班级作为Key,其他数据作为Value写出

        修改Map端输出为StudentMap出现问题：
            java.lang.Exception: java.io.IOException: Initialization of all the collectors failed. Error in last collector was:java.lang.NullPointerException
	            Caused by: java.io.IOException: Initialization of all the collectors failed. Error in last collector was:java.lang.NullPointerException

        原因：
            StudentMap 在网络传输过程中，没有经过序列化

        解决方案：
            StudentMap 去继承 Writable 实现序列化和反序列化方法


     */

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, StudentMap>.Context context) throws IOException, InterruptedException {
//        super.map(key, value, context);

        // 1500100001	施笑槐,22,女,文科六班,406
        String[] split = value.toString().split("\t");
        String[] cols = split[1].split(",");
        String clazz = cols[3];

        // public StudentMap(String id, String name, int age, String gender, String clazz, int score)
        StudentMap studentMap = new StudentMap(split[0], cols[0], Integer.parseInt(cols[1]), cols[2], cols[3], Integer.parseInt(cols[4]));

        context.write(new Text(clazz),studentMap);

    }
}
